'use strict';

const moment = require('moment');

module.exports = app => {
  const { Sequelize } = app;

  const IntegralOuts = app.model.define('IntegralOuts', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: Sequelize.INTEGER
    },
    userId: {
      type: Sequelize.INTEGER,
      allowNull: false,
      onDelete: "CASCADE",
      onUpdate: "CASCADE",
      references: {
        model: 'Users',
        key: 'id'
      }
    },
    goodsId: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'Goods',
        key: 'id'
      },
      defaultValue: 0
    },
    total: {
      allowNull: false,
      type: Sequelize.INTEGER,
      comment: '总和'
    },
    number: {
      allowNull: false,
      type: Sequelize.INTEGER,
      comment: '支出'
    },
    type: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      comment: '0：种草',
    },
    createdAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm') }
    },
    updatedAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm') }
    },
  }, {
    paranoid: false,
    charset: 'utf8mb4',
    collate: 'utf8mb4_general_ci',
    indexes: [{
      fields: ['userId', 'goodsId']
    }],
    comment: '幸运草支出表'
  });

  return IntegralOuts;

};